<?php
/**
 *
 *
 * @author raphael seebacher <raphasee@ee.ethz.ch>
 * @version 0.01
 */
abstract class web_view extends view {
    protected $template = '';
    protected $stylesheets = Array();
    protected $scripts = Array();
    protected $user_information = NULL;

    final protected static function get_template_file($name) {
        return file_get_contents('../templates/'.$name.'.html');
    }

    final protected function set_stylesheets_in_template() {
        $result = '';

        foreach ($this->stylesheets as $stylesheet) {
            $result .= '<link rel="stylesheet" type="text/css" '
                .'href="/stylesheets/'.$stylesheet.'.css" />'."\n";
        }

        $this->template = str_replace('[stylesheets]', $result, $this->template);
    }

    final protected function set_scripts_in_template() {
        $result = '';

        foreach ($this->scripts as $script) {
            $result .= '<script type="text/javascript" src="';
            if (substr($script, 0, 5) == 'https') {
                $result .= $script;
            }
            else {
                $result .= '/javascript/'.$script.'.js';
            }
            $result .= '"></script>';
        }

        $this->template = str_replace('[scripts]', $result, $this->template);
    }

    final protected function set_menu_in_template() {
        if ($this->user_information == NULL) {
            $menu_template = self::get_template_file('menu_loggedout');
        }
        else {
            $menu_template = self::get_template_file('menu_loggedin');
        }

        $this->template = str_replace('[menu]', $menu_template, $this->template);
    }

    final protected function set_content_in_template() {
        $this->template = str_replace(
          '[content]',
          $this->get_page_content_template(),
          $this->template);

        if ($this->user_information != NULL) {
            foreach ($this->user_information as $key => $value) {
                $this->template = str_replace('['.$key.']', $value, $this->template);
            }
        }
    }

    final public function set_user_information($user_information) {
        $this->user_information = $user_information;
    }

    abstract protected function get_page_content_template();

    public function send_header() {

    }

    public function get_content() {
        $this->template = self::get_template_file('page');

        $this->set_stylesheets_in_template();
        $this->set_scripts_in_template();
        $this->set_menu_in_template();
        $this->set_content_in_template();

        return $this->template;
    }
}
?>